<template>
  <div class="header">
    <div class="container">
      <h1 @click="homefn" class="login"></h1>
      <ul class="app_header_nav">
        <li @click="homefn">首页</li>
        <li v-for="item in list" :key="item.id">
          <span @click="handelclick(item.name)">{{ item.name }}</span>
          <div class="box-card">
            <el-card class="card" label="ttb">
              <div class="card_pr">
                <div
                  class="item_box"
                  v-for="item in item.children"
                  :key="item.id"
                >
                  <img :src="item.picture" alt="" />
                  <span>{{ item.name }}</span>
                </div>
              </div>
            </el-card>
          </div>
        </li>
      </ul>

      <div class="search">
        <span class="el-icon-search"></span>
        <input type="text" placeholder="搜一搜" style="color: #e7e7e7" />
      </div>

      <div class="curr">
        <span class="el-icon-shopping-cart-2" @click="cartclick"></span>
        <span class="badge">0</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
    };
  },
  created() {
    this.getCategory();
  },
  methods: {
    homefn() {
      this.$router.push("/home/homepage");
    },
    handelclick(val) {
      // console.log(val);
      this.$router.push({ name: val });
    },
    cartclick() {
      this.$router.push("/cart");
    },
    // 导航栏数据
    async getCategory() {
      const res = await this.$http.category();
      this.list = res.data.result;
      if (this.$store.getters.getToken) {
        this.onShow = true;
      } else {
        this.onShow = false;
      }
    },
  },
};
</script>

<style lang = "scss" scoped>
.header {
  width: 1240px;
  height: 132px;
  padding: 0 140px;
  background-color: #fff;
  margin: 0 auto;
  .container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: relative;
    .login {
      width: 200px;
      height: 132px;
      background: url(http://erabbit.itheima.net/img/logo.0940ebb5.png)
        no-repeat center 18px / contain;
      cursor: pointer;
    }
    .box-card {
      display: none;
    }
    .app_header_nav {
      display: flex;
      justify-content: space-between;
      li {
        margin-right: 40px;
        width: 38px;
        text-align: center;
        box-sizing: border-box;
        font-size: 16px;
        line-height: 32px;
        height: 32px;
        display: inline-block;
        span {
          cursor: pointer;
        }
        .box-card {
          width: 1240px;
          position: absolute;
          left: 0;
          top: 120px;
          z-index: 101;
          .card {
            box-sizing: border-box;
            background-color: #fff;
            box-shadow: 0 0 6px 1px rgba(204, 204, 204, 0.553);
            .card_pr {
              display: flex;
              justify-content: start;
              align-items: center;
              flex-wrap: wrap;
              font-size: 14px;
            }
            .item_box {
              width: 110px;
              height: 90px;
              display: flex;
              flex-direction: column;
              justify-content: space-between;
              align-items: center;
              cursor: pointer;
              img {
                width: 60px;
                height: 60px;
              }
              span {
                padding-top: 10px;
              }
            }
            .item_box:hover {
              color: #27ba9b;
            }
          }
        }
      }
      li:hover {
        border-bottom: 1px solid #27ba9b;
      }
      li:hover .box-card {
        display: block;
      }
      li:first-child:hover .box-card {
        display: none;
      }
    }
    .search {
      width: 170px;
      height: 32px;
      border-bottom: 1px solid #e7e7e7;
      position: relative;
      line-height: 32px;

      display: flex;
      align-items: center;
      span {
        width: 18px;
        height: 18px;
      }
      input {
        outline: none;
        border: none;
        width: 140px;
        margin-left: 15px;
        padding-left: 5px;
        color: #e7e7e7;
        font-size: 14px;
      }
    }
    .curr {
      width: 50px;
      height: 32px;
      position: relative;
      text-align: center;
      box-sizing: border-box;
      padding-top: 4px;
      span {
        font-size: 26px;
      }
      .badge {
        font-size: 12px;
        position: absolute;
        top: -5px;
        right: 0;
        background-color: #e26237;
        border-radius: 10px;
        color: #fff;
        padding: 1px 4px;
      }
    }
  }
}
</style>